/**
 * Captain
 * © 2018 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: AppraiseServiceImpl.java
 * 创  建  人: QinYoubin (qin.youbin@sccaptain.com.cn)
 * 创建时间: 2018年4月27日 上午10:18:26
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.message.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.text.DefaultEditorKit.BeepAction;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.cpt.framework.core.OperationResult;
import com.material.c2c.core.UUIDGenerator;
import com.material.c2c.message.dao.AppraiseDao;
import com.material.c2c.message.entity.YHPL;
import com.material.c2c.message.model.OrderMessageDTO;
import com.material.c2c.shoping.dao.OrderFormMapDao;
import com.material.c2c.shoping.entity.DD_SP_MAP;

import cn.hutool.core.bean.BeanUtil;

/**
 * 用户评论service
 * @since 1.0
 * @version 1.0
 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
 */
@Service
public class AppraiseServiceImpl implements AppraiseService{
  @Resource
  private AppraiseDao appraiseDao;
  @Resource
  private OrderFormMapDao orderFormMapDao;
  @Override
  @Transactional
  public OperationResult add(YHPL yhpl) {
    yhpl.setId(UUIDGenerator.generateUUIDStr());
    yhpl = appraiseDao.add(yhpl);
    if(yhpl==null) {
      return OperationResult.failed("评论失败！");
    }
    return OperationResult.SUCCEED;
  }
  @Override
  @Transactional
  public OperationResult saveOrderAppraise(OrderMessageDTO dto) {
	  YHPL yhpl = dto.getYhpl();
	  yhpl.setPlsj(new Date());
	  yhpl.setSfsctp(2);
	  List<DD_SP_MAP> sps = orderFormMapDao.findByDdid(dto.getDdid());
	  for(DD_SP_MAP map:sps) {
		  YHPL addpl = new YHPL();
		  BeanUtil.copyProperties(yhpl, addpl);
		  addpl.setId(UUIDGenerator.generateUUIDStr());
		  addpl.setSpid(map.getSpid());
		  appraiseDao.add(addpl);
	  }
	  return OperationResult.SUCCEED;
  }
  @Override
  @Transactional
  public OperationResult update(YHPL yhpl) {
    yhpl = appraiseDao.update(yhpl);
    if(yhpl==null) {
      return OperationResult.failed("评论失败！");
    }
    return OperationResult.SUCCEED;
  }
  
  @Override
  @Transactional
  public OperationResult delete(String id) {
    appraiseDao.delete(id);
    return OperationResult.SUCCEED;
  }
  @Override
  public Page<YHPL> page(String spid,Pageable pageable){
    //return appraiseDao.findBySpidOrderByPlsjDesc(spid, pageable);
	  return appraiseDao.findAll(pageable);
  }
  @Override
  public List<YHPL> top5(String spid){
    return appraiseDao.findTop10BySpidOrderByPlsjDesc(spid);
  }
}
